import router from './router'
import store from './store'
import { getToken } from '@/utils/auth'

const whiteList = ['/login','/register']

router.beforeEach((to, from, next) => {
  if (getToken()) {
    if (to.path === '/login' ) {
      next({ path: '/' })
    } else {
		const hasGetUserInfo = store.getters.name
		if (hasGetUserInfo) {
		  next()
		} else {
		  try {
		    // get user info
		     store.dispatch('GetUserInfo')
		  
		    next()
		  } catch (error) {
		    // remove token and go to login page to re-login
		     store.dispatch('user/resetToken')
		    Message.error(error || 'Has Error')
		    next(`/login?redirect=${to.path}`)
		    NProgress.done()
		  }
		}
      // next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，或不需要验证的直接进入
      next()
    } else if (to.meta.auth === true) { // 需要验证的重定向到登录页
      next(`/login?redirect=${to.path}`)
    } else {
      next()
    }
  }
})

router.afterEach(() => {

})
